﻿using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO;
using System.Net;
using System.Text;

namespace NetduinoPlusWebServer
{
    public class Program
    {
        const string WebFolder = "\\SD\\Web";
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        public static void Main()
        {

            Listener webServer = new Listener(RequestReceived);

           
            while (true)
            {
                // Blink LED to show we're still responsive
                //led.Write(!led.Read());
                Thread.Sleep(500);
            }

        }

        public static void pisca()
        {
            led.Write(false);
            Thread.Sleep(300);
            led.Write(true);
            Thread.Sleep(300);
            led.Write(false);
            Thread.Sleep(300);
            led.Write(true);
        }

        private static void RequestReceived(Request request)
        {

            

            // Use this for a really basic check that it's working
            //request.SendResponse("nada", "text/html");
            request.SendResponse("nada-" + DateTime.Now.ToString());
            pisca();
           
            // Send a file
            //TrySendFile(request);

        }

        /// <summary>
        /// Look for a file on the SD card and send it back if it exists
        /// </summary>
        /// <param name="request"></param>
        private static void TrySendFile(Request request)
        {
            // Replace / with \
            string filePath = WebFolder + request.URL.Replace('/', '\\');

            if (File.Exists(filePath))
                request.SendFile(filePath);
            else
                request.Send404();
        }

    }
}
